﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.User
// Project Description :    
// ===============================================================================
// Class Name          :    AuthenticationServiceImpl
// Namespace:          :    Gfw.BLL.Basic.User
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/27 21:35:30
// Update Time         :    2012/6/27 21:35:30
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Admin;
using Gfw.Repositories.Admin;
using L.Basic.Authority.Context;
using L.Basic.Authority.Models;
using L.Basic.Authority.Services;

namespace Gfw.BLL.Basic.User
{
    public class AuthenticationServiceImpl : IAuthenticationService
    {
      

        public L.Basic.Authority.Models.UserLogin GetLoginInfo(string userId)
        {
            var rep = new UsrUserLoginRepository();
            var permUserLogin = rep.Get(userId);
            if (permUserLogin == null)
                return null;
            UserLogin result = new UserLogin()
            {
                Userid = permUserLogin.Userid,
                UserOnline = permUserLogin.IsUserOnline,
                LastLoginIp = permUserLogin.LastLoginip,
                LastLoginTime = permUserLogin.LastLoginDate,
                LoginIp = permUserLogin.Loginip,
               // LoginName = permUserLogin.,
                LoginTime = permUserLogin.LoginDate
            };
            return result;
        }

        public void UpdateLoginInfo(L.Basic.Authority.Models.UserLogin userLogin, string loginIp, DateTime loginTime)
        {
            userLogin.LastLoginIp = userLogin.LoginIp;
            userLogin.LastLoginTime = userLogin.LoginTime;

            userLogin.LoginIp = loginIp;
            userLogin.LoginTime = loginTime;
            var rep = new UsrUserLoginRepository();

            UsrUserlogin permUserLogin = new UsrUserlogin()
            {
                LastLoginip = userLogin.LastLoginIp,
                LastLoginDate = userLogin.LastLoginTime,
                Loginip = userLogin.LoginIp,
               // LoginName = userLogin.LoginName,
                LoginDate = userLogin.LoginTime,
                Userid = userLogin.Userid,
                IsUserOnline = userLogin.UserOnline
            };
            rep.SaveOrUpdate(permUserLogin);
        }

        public void UpdateLoginInfo(string userId, string loginIp, DateTime loginTime)
        {
            var userLogin = GetLoginInfo(userId);
            UpdateLoginInfo(userLogin, loginIp, loginTime);
        }

        public bool Login(string loginName, string password, out L.Basic.Authority.Context.Context context)
        {
            context = null;
           L.Basic.Authority.Models.User user;
            var rep = new UsrUserRepository();
            if (rep.Validate(loginName, password, out user))
            {
                context = new Context()
                {
                    CurrentUser = user
                };
                //获取权限信息
               // context.Permissions = GetModulePermission(user.Roles);
                return true;
            }
            return false;
        }

        public IList<L.Basic.Authority.Models.ModulePermission> GetModulePermission(IList<L.Basic.Authority.Models.Role> roles)
        {
            throw new NotImplementedException();
        }

        public void Logout()
        {
            throw new NotImplementedException();
        }

        public bool ChangePassword(string loginName, string oldPassword, string newPassword)
        {
            UsrUserRepository repository = new UsrUserRepository();
            return repository.ChangePassword(loginName, oldPassword, newPassword);
        }
    }
}
